<template>
    <div class="login-container">
        <el-form :model="form" ref="ruleFormRef" label-width="auto" :rules="rules" class="login-form"  status-icon>
            <el-form-item label="Password" prop="pass">
                <el-input v-model="form.password" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item label="Confirm Password" prop="checkPass">
                <el-input v-model="form.confirmPassword" type="password" autocomplete="off" />
            </el-form-item>
            <el-form-item >
                <el-button type="primary" @click="submitForm(ruleFormRef)">Create Account</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
import { invoke } from '@tauri-apps/api/tauri';
import { FormRules, FormInstance } from 'element-plus';

const router = useRouter();
const ruleFormRef = ref<FormInstance>()

const form = reactive({
    password: '',
    confirmPassword: ''
});

const validatePass = (rule: any, value: any, callback: any) => {
    console.log('validatePass =>', value)
    
  if (value === '') {
    callback(new Error('Please input the password'))
  } else {
    if (form.password !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPass')
    }
    callback()
  }
}

const rules = reactive<FormRules<typeof form>>({
    password: [{ validator: validatePass, trigger: 'blur' }],
    confirmPassword: [{validator: validatePass, trigger: 'blur'}],
});

function submitForm(formEl: FormInstance | undefined) {
    if (!formEl) return
    formEl.validate((valid) => {
      if (valid) {
        console.log('submit!')
      } else {
        console.log('error submit!')
      }
    })
}

function createAccount() {
    invoke('create_account', { password: form.password }).then(() => {
        // router.push('/login');
        console.log('account created :', form.password);
        
        router.push('create_account');
    });
}

</script>

<style lang="scss" scoped>


</style>